package com.blue.waffle.iwc.ui;

import java.sql.SQLException;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.scenes.scene2d.EventListener;
import com.badlogic.gdx.scenes.scene2d.InputEvent;
import com.badlogic.gdx.scenes.scene2d.ui.Dialog;
import com.badlogic.gdx.scenes.scene2d.ui.Skin;
import com.badlogic.gdx.scenes.scene2d.ui.Table;
import com.badlogic.gdx.scenes.scene2d.ui.TextButton;
import com.blue.waffle.iwc.domains.Difficulties;
import com.blue.waffle.iwc.domains.Difficulty;
import com.blue.waffle.iwc.domains.Level;
import com.blue.waffle.iwc.domains.Profile;
import com.blue.waffle.iwc.game.IWCGame;
import com.blue.waffle.iwc.screens.AbstractScreen;
import com.blue.waffle.iwc.screens.DifficultyScreen;
import com.blue.waffle.iwc.screens.GameScreen;
import com.blue.waffle.iwc.screens.MenuScreen;

public class GameMenuDialog extends Dialog {

	private GameScreen screen;
	public static final int TYPE_MENU=2;
	public static final int TYPE_GAMEOVER=4;
	public static final int TYPE_GAMEOVER_EASY=8;
	public static final int TYPE_FEEDBACK=16;
	public static final int TYPE_LEVEL_CLEARED=32;
	private int dialogType=TYPE_MENU;
	
	public GameMenuDialog(String title, Skin skin, GameScreen screen,int type) {
		super(title, skin);
		this.screen=screen;
		this.dialogType=type;
		setModal(true);
		setMovable(false);
		setupMenu();
	}
	
	private void setupMenu() {
		switch (dialogType) {
		case TYPE_MENU:
			setupMenuButtons();
			break;
		case TYPE_GAMEOVER:
			setupGameOverButtons();
			break;
		case TYPE_LEVEL_CLEARED:
			setupLevelClearButtons();
			break;
		default:
			break;
		}
	}
	
	private void setupLevelClearButtons(){
		Skin skin=screen.getUiSkin();
		
		ButtonActorListener listener= new ButtonActorListener(screen,this){
			@Override
			public void clicked(InputEvent event, float x, float y) {
				super.clicked(event, x, y);
				TextButton button=(TextButton)event.getListenerActor();
				String btnName=button.getText().toString();
				IWCGame game =screen.getGame();
				if (btnName.equals("Next Word")) {
					game.setScreen(new GameScreen(game, game.getGameManager().getCurrentDifficulty()));
				}else if (btnName.equals("Clear Difficulty")) {
					Profile profile=game.getGameManager().getUserProfile();
					profile.setCurrentDifficulty(new Difficulty());
					profile.setLastPlayedLevel(-1);
					try {
						game.getGameManager().setUserProfile(profile).PersistProfile();
						game.setScreen(new DifficultyScreen(game));
					} catch (SQLException e) {
						e.printStackTrace();
					}
					
				}
				else {
					try {
						game.getGameManager().PersistProfile();
						if (btnName.equals("Back to Menu")) {
							game.setScreen(new MenuScreen(game));
						}else {
							Gdx.app.exit();
						}
					} catch (SQLException e) {
						e.printStackTrace();
					}
				}
			}
		};
		
		
		Level level=screen.getLevel();
		
		Table table=getContentTable();
		table.row().center();
		if (screen.getDifficultyRules()==Difficulties.EASY) {
			table.add("Time Played:");
			table.add(level.getPlayTime()+"");
		}else {
			table.add("Time Left:");
			table.add((screen.getTimeLimit()-level.getPlayTime())+"");
		}
		
		table.row().center();
		table.add("Score:");
		table.add(level.getScore()+"");
		
		TextButton nextBtn=null;
		if (screen.getGame().getGameManager().getNextLevel()==null) {
			nextBtn=new TextButton("Clear Difficulty",skin);
		}else {
			nextBtn=new TextButton("Next Word",skin);
		}
		
		TextButton backToMenuBtn=new TextButton("Back to Menu",skin);
		TextButton exitBtn=new TextButton("Exit Game",skin);
		createButtons(new TextButton[]{nextBtn,backToMenuBtn,exitBtn}, listener,2);
		
		
	}
	
	private void setupGameOverButtons(){
		Skin skin=screen.getUiSkin();
		
		ButtonActorListener listener= new ButtonActorListener(screen,this){
			@Override
			public void clicked(InputEvent event, float x, float y) {
				super.clicked(event, x, y);
				TextButton button=(TextButton)event.getListenerActor();
				String btnName=button.getText().toString();
				if (btnName.equals("Try Again")) {
					((GameScreen)screen).useAttempt();
					((GameMenuDialog)getData()).hide();
				}else if (btnName.equals("Start Over")) {
					IWCGame game =screen.getGame();
					game.getGameManager().resetDifficulty();
					game.setScreen(new GameScreen(game, game.getGameManager().getCurrentDifficulty()));
				}
				else {
					try {
						screen.getGame().getGameManager().PersistProfile();
						if (btnName.equals("Back to Menu")) {
							screen.getGame().setScreen(new MenuScreen(screen.getGame()));
						}else {
							Gdx.app.exit();
						}
					} catch (SQLException e) {
						e.printStackTrace();
					}
				}
			}
		};
		TextButton retryBtn=null;
		if (screen.getAttempts()>0) {
			retryBtn=new TextButton("Try Again", skin);
		}else {
			retryBtn=new TextButton("Start Over", skin);
		}
		
		TextButton backToMenuBtn=new TextButton("Back to Menu",skin);
		TextButton exitBtn=new TextButton("Exit Game",skin);
		createButtons(new TextButton[]{retryBtn,backToMenuBtn,exitBtn}, listener);
		
		
	}
	
	private void createButtons(TextButton[] btns, EventListener listener) {
		createButtons(btns, listener,1);
	}
	
	private void createButtons(TextButton[] btns, EventListener listener,int colSpan) {
		if (btns!=null && listener!=null) {
			Table contenTable=getContentTable();
			contenTable.defaults().size(IWCGame.VIRTUAL_WIDTH/3, 50).padBottom(5);
			
			for (TextButton textButton : btns) {
				textButton.addListener(listener);
				contenTable.row();
				contenTable.add(textButton).colspan(colSpan);
			}
		}
	}
	
	private void setupMenuButtons(){
		
		Skin skin=screen.getUiSkin();
		
		ButtonActorListener menuBtnListener= new ButtonActorListener(screen,this){
			@Override
			public void clicked(InputEvent event, float x, float y) {
				super.clicked(event, x, y);
				TextButton button=(TextButton)event.getListenerActor();
				String btnName=button.getText().toString();
				if (btnName.equals("Continue")) {
					((GameMenuDialog)getData()).hide();
				}else {
					try {
						screen.getGame().getGameManager().PersistProfile();
						if (btnName.equals("Back to Menu")) {
							screen.getGame().setScreen(new MenuScreen(screen.getGame()));
						}else {
							Gdx.app.exit();
						}
					} catch (SQLException e) {
						e.printStackTrace();
					}
				}
			}
		};
		
		TextButton continueBtn=new TextButton("Continue", skin);
		TextButton backToMenuBtn=new TextButton("Back to Menu",skin);
		TextButton exitBtn=new TextButton("Exit Game",skin);
		
		createButtons(new TextButton[]{continueBtn,backToMenuBtn,exitBtn}, menuBtnListener);
		
	}

	public AbstractScreen getScreen() {
		return screen;
	}
	
	@Override
	public void hide() {
		((GameScreen)screen).setStop(false);
		super.hide();
	}

}
